<?php
use app\common\helper\appmsg_helper;
use app\appmsg\config\appmsg_config;
?>
{include file="../app/common/admin/lib/header.html" title="图文管理"}
  <div class="page-header border-bottom clearfix">
    <h2 class="left">图文管理<span>在这里对图文进行管理</span></h2>
    {/*各店铺只能添加单图文，总店铺才能添加多图文*/}
    <a href="{:url('/appmsg/add/2')}" class="btn btn-success btn-sm right">添加多图文</a>
    <a href="{:url('/appmsg/add/1')}" class="btn btn-success btn-sm right">添加单图文</a>
    <a href="javascript:void(0);" class="btn btn-default btn-sm right js_advancedSearchBtn">搜索</a>
  </div>
  <div class="search-form js_advancedForm" {if !$search_filter}style="display:none;"{/if}>
    <form action="/appmsg/admin" method="get" class="js_appmsgSearchForm">
      <input type="hidden" name="status" value="{$status}">
      <input type="hidden" name="need_store_id" value="{$need_store_id}">
      <input type="hidden" name="type" value="{$type}">
      <input type="hidden" name="verify" value="{$verify}">
      <input type="hidden" name="area_id" value="{$area_id}">
      <p>
        <label for="" class="search-f-label">标题：</label>
        <input type="text" class="search-f-ipt1" name="search_filter[title]" value="{$search_filter.title}">
      </p>
      <p>
      <label for="" class="search-f-label">添加时间：</label>
        <input type="text" class="search-f-ipt1 search-f-ipt2" name="search_filter[start_date]" value="{$search_filter.start_date}" id="startDate">
        <span class="search-f-line"></span>
        <input type="text" class="search-f-ipt1 search-f-ipt2" name="search_filter[stop_date]" value="{$search_filter.stop_date}" id="stopDate">
      </p>
      <p><label for="" class="search-f-label"></label><button type="submit" class="btn btn-success btn-xs js_searchBtn">搜索</button></p>
    </form>
  </div>
  
  <!--{/*让各店铺能看到总店铺将要推送的图文*/}-->
  <!--{if $store_id >= 1}-->
    <!--{$admin_appmsg_info = appmsg_helper::get_store_last_appmsg_info(0)}-->
    <!--{if $admin_appmsg_info}-->
    <!--<div class="alert add-form-title">-->
      <!--<strong>总后台最新图文素材</strong>-->
    <!--</div>-->
    <!--<table class="list-table">-->
      <!--<thead>-->
        <!--<tr>-->
          <!--<th style="width:100px;">封面图</th>-->
          <!--<th style="width:220px;">图文标题</th>-->
          <!--<th style="width:120px;">添加时间</th>-->
          <!--<th>操作</th>-->
        <!--</tr>-->
      <!--</thead>-->
      <!--<tbody>-->
          <!--<tr data-id="{$admin_appmsg_info.id}" class="bg-gray">-->
            <!--<td><img src="{_image($admin_appmsg_info.cover, 'small')}" width="100" /></td>-->
            <!--<td>-->
              <!--<div><span style="color:#b1b1b1;">[1]</span> {$admin_appmsg_info.title}</div>-->
              <!--{if $admin_appmsg_info.type == 2}-->
                <!--{$son_appmsg_list = appmsg_helper::get_merge_appmsg_list($admin_appmsg_info.id)}-->
                <!--{foreach $son_appmsg_list as $k1=>$v1}-->
                <!--<div><span style="color:#b1b1b1;">[{$k1+2}]</span> {$v1.title}</div>-->
                <!--{/foreach}-->
              <!--{/if}-->
            <!--</td>-->
            <!--<td>{time_format($admin_appmsg_info.add_time)}</td>-->
            <!--<td>-->
                <!--<a href='javascript:void(0);' data-id="{$admin_appmsg_info.id}" class="btn btn-default btn-xs js_previewBtn">预览</a>-->
            <!--</td>-->
          <!--</tr>-->
      <!--</tbody>-->
    <!--</table>-->
    <!--<div style="margin-bottom:20px;"></div>-->
    <!--<div class="alert add-form-title">-->
      <!--<strong>本店铺图文素材</strong>-->
    <!--</div>-->
    <!--{/if}-->
  <!--{/if}-->
  
  <div class="filter-box">按状态：
    <a href='{:url("/appmsg/admin/index",["status"=>1])}' {if $status == 1} class="label label-primary" {else} class="label label-default" {/if}>正常</a>
    <a href='{:url("/appmsg/admin/index",["status"=>0])}' {if $status == 0} class="label label-primary" {else} class="label label-default" {/if}>已删除</a>
  </div>

  <!--start:列表-->
  <table class="list-table">
    <thead>
      <tr>
        <th style="width:20px;"><input type="checkbox" class="selectAll"></th>
        <th style="width:100px;">封面图</th>
        <th style="width:220px;">图文标题</th>
        <th style="width:100px;">是否审核</th>
        <th style="width:120px;">是否推送过</th>
        <th style="width:100px;">发布途径</th>
        <th style="width:80px;">阅读/转发</th>
        <th style="width:130px;">添加时间</th>
        <th>操作</th>
      </tr>
    </thead>
    <tbody>
    {if isset($list['data'])}
      {foreach $list['data'] as $k=>$v}
        <tr id="dataList{$v.id}" {if $k%2 == 1} class="bg-gray" {/if}>
          <td><input type="checkbox" class="listSelect"></td>
          <td>
            {if $v.store_id == 0}
            <img src="{:_image($v.cover, 'small')}" width="100" />
            {else}
            <img src="{:_image($v.cover, 'small_equal')}" width="100" />
            {/if}
          </td>
          <td>
          <div><span style="color:#b1b1b1;">[1]</span> {$v.title}</div>
          {if $v.type == 2}
          {$son_appmsg_list = appmsg_helper::get_merge_appmsg_list($v.id)}
          {foreach $son_appmsg_list as $k1=>$v1}
          <div class="js_preSonItem" data-id="{$v1.id}"><span style="color:#b1b1b1;">[{$k1+2}]</span> {$v1.title}</div>
          {/foreach}
          {/if}
          </td>

          <td>{if $v.verify >= 1}{:appmsg_config::$verify_relation[$v['verify']]}{else /}--{/if}
          {if $v.verify == 3}
              {$verify_record_info = appmsg_helper::get_verify_record_info($v.id)}
              {if $verify_record_info && $verify_record_info.reason}
              <div style="color:#aaa;"><span style="color:#bbb;">原因：</span>{$verify_record_info.reason}</div>
              {/if}
          {/if}
          </td>

          <td><div class="js_preSyncStatus">{if $v.send == 1}已推送过<br>{$v.send_time|time_format}{else}--{/if}</div></td>
          <td>后台</td>
          <td>阅读：{$v.view_num}<br>转发：{$v.rt_num}
          </td>
          <td>{$v.add_time|time_format}</td>

          <td>
            {if $status == 1}
              <a href='javascript:void(0);' data-store="{$v.store_id}" data-id="{$v.id}" class="btn btn-default btn-xs js_previewBtn">预览</a>
              <!--{*管理员才有做审核与同步操作*}-->
              <a href='javascript:void(0);' data-id="{$v.id}" class="btn btn-success btn-xs js_syncBtn" style="">同步到公众号</a>
              {if $v.verify == 1}
                {if $store_id == 0 && $v.store_id >= 1}
                <a href='javascript:void(0);' data-id="{$v.id}" data-verify="3" class="btn btn-default btn-xs js_verifyBtn">打回</a>
                {/if}
              {elseif $v.verify == 2 /}
                {if $store_id == 0}
                <div class="s-users-add js_selectWrap">
                  <span data-status="1" class="js_currSelect">审核处理<em></em></span>
                  <div class="s-users-add-box js_selectList" style="display:none;">
                    <ul>
                      <li class="js_verifyBtn" data-id="{$v.id}" data-verify="1">通过</li>
                      <li class="js_verifyBtn" data-id="{$v.id}" data-verify="3">打回</li>
                    </ul>
                  </div>
                </div>
                {/if}
              {else /}
                {if $store_id >= 1}
                <a href='javascript:void(0);' data-id="{$v.id}" data-verify="2" class="btn btn-warning btn-xs js_verifyBtn">提交审核</a>
                {/if}
              {/if}

            <!--{*后台管理员随时可编辑，店铺管理员只能在未提交和打回后可编辑*}  href="{:url("/open_card/admin/edit",["id" => $v.id])}"-->
            {if $store_id || ($store_id >= 1 && $v.verify >= 3)}

              <a {$type = $v['type']} href="{:url("/appmsg/add/$type",["id" => $v.id])}" class="btn btn-info btn-xs">编辑</a>
              <a href='{:url("/appmsg/delete/admin")}' class="btn btn-danger btn-xs deleteOne">删除</a>
            {/if}
            {elseif $status == 0 /}
            <a href='{:url("/appmsg/admin/change_status",["status"=>1])}' class="btn btn-success btn-xs changeStatus"><i class="icon-trash icon-white"></i>恢复</a>

            {/if}
          </td>
        </tr>
        {/foreach}
    {else /}
          <tr><td class="txtleft" colspan="9">暂无数据</td></tr>
    {/if}
    </tbody>
    <tfoot><tr><td><input type="checkbox" class="selectAll"></td><td colspan="8"><a href='{:url("/appmsg/delete/admin")}' class="btn btn-danger btn-xs deleteAll">批量删除</a></td></tr></tfoot>
  </table>

<div id="pages"></div>

<!--end:列表-->
{js href="./static/js/module/appmsg.js"}
<script>
$(function(){
  // 高级搜索时间验证
  $('.js_searchBtn').click(function(e){
    e.preventDefault();
    var thisObj   = $(this);
    var startDate = $.trim($('#startDate').val());
    var stopDate  = $.trim($('#stopDate').val());
    if (startDate > stopDate) {
      showMsg('结束时间应大于等于开始时间');
      return;
    }
    thisObj.closest('form').submit();
  });
  
  // 更改分类
  $(".js_verifyBtn").live('click', function(e){
    e.preventDefault();
    
    var thisObj = $(this);
    var verify = thisObj.attr('data-verify');
    var selectId = $(this).closest("tr").attr("id").substring(8);
    
    if (!selectId) {
      showMsg('请选择要操作记录');
      return false;
    }
    
    var postUrl  = siteUrl+'/appmsg/admin/verify';
    var postData = { 'id':selectId, 'verify':verify };
    
    if (thisObj.attr('is-post') == 1) {
      showMsg('数据正在提交中，请耐心等待');
      return false;
    }
    thisObj.attr('is-post', 1);
    
    showConfirm('确定要执行此操作吗？', 1, function(){
      
      // 如果是打回，填写打回原因
      if (verify == 3) {
        var contentHtml = '<div>';
        contentHtml += '<div style="padding:10px 13px;">请填写打回原因</div>';
        contentHtml += '<div style="padding-left:13px;"><textarea name="verify_reason" style="width:260px; height:80px;"></textarea></div>';
        contentHtml += '</div>';
        var content    = $(contentHtml);
        var DD = ace.dialog({
          'title':'微信扫码预览',
          'content':content,
          'init':function(){
            content.closest('.aceDialog').css('width', '300px');
            //content.closest('.aceDialog').find('.okBtn').css('margin-left', '30px');
          },
          'ok':function(){
            var reason = $.trim($(":input[name='verify_reason']").val());
            if (!reason) {
              thisObj.attr('is-post', 0);
              showMsg('请填写打回的原因');
              return false;
            }
            
            postData['reason'] = reason;
            postVerify(thisObj, postUrl, postData, function(){
              DD.close();
            });
          },
          'close':function(){
            thisObj.attr('is-post', 0);
          }
        });
        
        // 其它状态
      } else {
        postVerify(thisObj, postUrl, postData);
      }
    }, function(){
      thisObj.attr('is-post', 0);
    });
  });
  
  /**
   * 审核处理
   * @param thisObj 当前操作的节点
   * @param postUrl post地址
   * @param postData post的数据
   * @param okFunc 成功后的回调
   */
  function postVerify(thisObj, postUrl, postData, okFunc)
  {
    $.post(postUrl, postData, function(json){
      if (json.info != 'ok') {
        thisObj.attr('is-post', 0);
        showMsg(json.info);
        return false;
      }
      
      thisObj.closest("tr").fadeOut(function(){
        $(this).remove();
        interLineColor();
      });
      
      if (okFunc && typeof okFunc == 'function') {
        okFunc();
      }
      
      //thisObj.closest("tr").find(".js_tycategory_idText").text(json.category_id_value);
      //thisObj.closest("tr").find(".js_selectList").hide();
    }, 'json').error(function(){
      thisObj.attr('is-post', 0);
    });
  }
  
  // 同步
  $(".js_syncBtn").click(function(e){
    e.preventDefault();
    
    var thisObj = $(this);
    var id = thisObj.attr('data-id');
    
    if (thisObj.attr('is-post') == 1) {
      showMsg('数据正在提交中，请耐心等待');
      return false;
    }
    thisObj.attr('is-post', 1);
    showConfirm('你确定要执行审核操作吗？', 1, function(){
      $.post(siteUrl+'/appmsg/sync/sync_news', { 'id':id }, function(json){
        thisObj.attr('is-post', 0);
        if (json.info != 'ok') {
          showMsg(json.info);
          return false;
        }
        
        showMsg('同步成功', 1);
        thisObj.closest('tr').find('.js_preSyncStatus').html('已同步<br>'+json.sync_time);
      }, 'json').error(function(){
        thisObj.attr('is-post', 0);
        showMsg('网络异常，请稍后再试');
      });
    }, function(){
      thisObj.attr('is-post', 0);
    });
  });

  // 页面复制
  var clip = new ZeroClipboard( $('.js_copyList'), {
    moviePath: "../../../../js/zclip/ZeroClipboard.swf"
  });
  clip.on( 'complete', function(client, args) {
    showMsg("复制成功，复制店铺为："+ args.text, 1);
  });
});
</script>
{include file="../app/common/admin/lib/footer.html"}